package me.stephenj.contactsweb.controller;


import me.stephenj.contactsweb.service.UploadService;
import me.stephenj.contactsweb.utils.CommonResult;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@CrossOrigin
@RequestMapping("/api")
public class UploadController {
    private static Logger logger = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private UploadService uploadService;

    @RequestMapping("uploadImage")
    @ResponseBody
    public CommonResult<String> uploadImage(@RequestParam(value = "file", required = true) MultipartFile file) {
        try {
            String pathName = uploadService.uploadImage(file);
            return CommonResult.successReturn("http://docs.sky.com/images/" + pathName);
        } catch (Exception e) {
            logger.error("uploadImager error ", e);
            return CommonResult.errorReturn("upload image error");
        }
    }
}
